﻿Public Class ImportImageControl
    Private m_InsertImage As New InsertImage
    Property InsertImage() As InsertImage
        Get
            Return Me.m_InsertImage
        End Get
        Set(ByVal value As InsertImage)
            Me.m_InsertImage = value
        End Set
    End Property
   
    Private m_SetPoint As Integer '

    Private prePoint As PointF '鼠标按下的点
    Private ReDrawSign As Boolean = False '鼠标松开后的重绘标志
    Public CloseForm As Boolean = False

    '属性----------------------------------------------
    Private boolMenu As Boolean
    Private boolDblClkSet As Boolean
    ''' <summary>
    ''' 鼠标移动的类型:0指针形，1十字形，2放大，3缩小，4移动。
    ''' </summary>
    ''' <remarks></remarks>
    Private m_MouseMoveType As Integer = 0 '

    

    Property SetPoint() As Integer '
        Get
            Return Me.m_SetPoint
        End Get
        Set(ByVal value As Integer)
            Me.m_SetPoint = value
        End Set
    End Property
    ''' <summary>
    ''' 鼠标移动的类型，如果鼠标为0，则为一般鼠标，如果为1，则用于定位,2等于放大，3为缩小，4为移动
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property MouseMoveType() As Integer
        Get
            Return Me.m_MouseMoveType
        End Get
        Set(ByVal value As Integer)
            Me.m_MouseMoveType = value
        End Set
    End Property
    ''' <summary>
    ''' 设置是否通过左键激活菜单
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Menu() As Boolean
        Get
            Return boolMenu
        End Get
        Set(ByVal value As Boolean)
            boolMenu = value
        End Set
    End Property
    ''' <summary>
    ''' 设置是否通过双击激活设置对话框
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DBlClkSet() As Boolean
        Get
            Return boolDblClkSet
        End Get
        Set(ByVal value As Boolean)
            boolDblClkSet = value
        End Set
    End Property
    Public Event InsertImageChanged(ByRef InsertImageTemp As InsertImage)
    Public Sub OnInsertImageChanged(ByRef InsertImageTemp As InsertImage)
        InsertImageTemp = Me.m_InsertImage.Clone
        RaiseEvent InsertImageChanged(InsertImageTemp)
    End Sub

    Private Sub ImportImageControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub SetMouseType(ByVal i As Integer)
        Me.m_MouseMoveType = i
        Select Case Me.m_MouseMoveType
            Case 0
                Me.Cursor = Cursors.Arrow
            Case 1
                Me.Cursor = Cursors.Cross '设置点位
            Case 2
                Me.Cursor = SetCursor(SetBitmap("zoom-in"), New PointF(0, 0)) 'New Cursor(My.Application.Info.DirectoryPath & "\ZoomIn.cur") '放大
            Case 3
                Me.Cursor = SetCursor(SetBitmap("zoom-out"), New PointF(0, 0)) 'New Cursor(My.Application.Info.DirectoryPath & "\ZoomOut.cur") '缩小
            Case 4 '移动
                Me.Cursor = Cursors.SizeAll '移动
            Case 5
                Me.Cursor = New Cursor(My.Application.Info.DirectoryPath & "\Care.cur") '关心点
            Case 6 '设置剪切的区域
                Me.Cursor = New Cursor(My.Application.Info.DirectoryPath & "\3dgarro.cur") '关心点

        End Select
        If Me.SetPoint = 1 Or Me.SetPoint = 2 Then
            Me.Cursor = Cursors.Cross '设置点位
        End If
    End Sub

    ''' <summary>
    ''' 返回函数
    ''' </summary>
    ''' <param name="flag"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetBitmap(ByVal flag As String) As Bitmap

        Dim m_bitmap As Bitmap
        If flag = "choose" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\choose.png")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决此问题", MsgBoxStyle.OkOnly, "警告")
            End Try
        ElseIf flag = "move" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\move.png")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决此问题", MsgBoxStyle.OkOnly, "警告")
            End Try
        ElseIf flag = "zoom-out" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\zoom-out.png")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决此问题", MsgBoxStyle.OkOnly, "警告")
            End Try
        ElseIf flag = "zoom-in" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\zoom-in.png")
                '  m_bitmap = New Bitmap(Me.GetType, "zoom-in.ico")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决此问题", MsgBoxStyle.OkOnly, "警告")
            End Try
        End If


        Return m_bitmap

    End Function
    ''' <summary>
    ''' 使用任何图片做鼠标指针
    ''' </summary>
    ''' <param name="CCursor"></param>
    ''' <param name="Point"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetCursor(ByVal CCursor As Bitmap, ByVal Point As System.Drawing.PointF) As Cursor
        Dim hotX As Integer = Point.X
        Dim hoty As Integer = Point.Y

        Dim myNewCursor As Bitmap = New Bitmap(Cursor.Size.Width * 2 - hotX, Cursor.Size.Height * 2 - hoty)

        Dim g As Graphics = Graphics.FromImage(myNewCursor)
        g.Clear(Color.FromArgb(0, 0, 0, 0))
        g.DrawImage(CCursor, CCursor.Size.Width - hotX, CCursor.Size.Height - hoty, CCursor.Size.Width, CCursor.Size.Height)

        Return New Cursor(myNewCursor.GetHicon())
        'Cursor = New Cursor(myNewCursor.GetHicon())
        g.Dispose()
        myNewCursor.Dispose()
        CCursor.Dispose()

    End Function


    ''' <summary>
    ''' 按倍数放大图形
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub zoomin(ByVal point As PointF)
        If Me.m_InsertImage.gScale < 0.001 Then
            Exit Sub
        End If
        Me.m_InsertImage.gScale = Me.m_InsertImage.gScale / 1.5
        Me.m_InsertImage.OriginX = Me.m_InsertImage.OriginX - (point.X - Me.m_InsertImage.OriginX) * (1.5 - 1)
        Me.m_InsertImage.OriginY = Me.m_InsertImage.OriginY - (point.Y - Me.m_InsertImage.OriginY) * (1.5 - 1)
        Me.Refresh()
        OnInsertImageChanged(Me.m_InsertImage)
    End Sub
    Public Sub zoomout(ByVal point As PointF)
        Me.m_InsertImage.gScale = Me.m_InsertImage.gScale * 1.5
        Me.m_InsertImage.OriginX = Me.m_InsertImage.OriginX - (point.X - Me.m_InsertImage.OriginX) * (1 / 1.5 - 1)
        Me.m_InsertImage.OriginY = Me.m_InsertImage.OriginY - (point.Y - Me.m_InsertImage.OriginY) * (1 / 1.5 - 1)
        Me.Refresh()
        OnInsertImageChanged(Me.m_InsertImage)
    End Sub
    Private Sub ImportImageControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left And Me.m_SetPoint = 1 Then '设置第一点
            Dim p As Point
            p.X = e.X
            p.Y = e.Y
            Me.m_InsertImage.Point1 = Me.m_InsertImage.ScreenToDraw(p)
            Me.m_InsertImage.Symbol1.SymbolSign.mSymbolsPoint = Me.m_InsertImage.PointToLocation(p) '设置绘图中的坐标
            Me.m_InsertImage.Symbol1.SymbolVisible = True
            Me.m_InsertImage.Symbol1.SymbolText.Name = "Point1"
        ElseIf e.Button = Windows.Forms.MouseButtons.Left And Me.m_SetPoint = 2 Then
            Dim p As Point
            p.X = e.X
            p.Y = e.Y
            Me.m_InsertImage.Point2 = Me.m_InsertImage.ScreenToDraw(p)
            Me.m_InsertImage.Symbol2.SymbolSign.mSymbolsPoint = Me.m_InsertImage.PointToLocation(p) '设置绘图中的坐标
            Me.m_InsertImage.Symbol2.SymbolVisible = True
            Me.m_InsertImage.Symbol2.SymbolText.Name = "Point2"
        End If
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If Me.m_MouseMoveType = 2 Then '放大
                Me.zoomin(New Point(e.X, e.Y))
            ElseIf Me.m_MouseMoveType = 3 Then '缩小
                Me.zoomout(New Point(e.X, e.Y))
            End If
            Dim point As New Point(e.X, e.Y)
            prePoint = point
        End If
        Me.Refresh()
        OnInsertImageChanged(Me.m_InsertImage)
    End Sub

    Private Sub ImportImageControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim dx As Single = e.X - prePoint.X
        Dim dy As Single = e.Y - prePoint.Y
        If Me.Cursor = Cursors.SizeAll And e.Button = Windows.Forms.MouseButtons.Left Then '移动
            Me.m_InsertImage.OriginX = Me.m_InsertImage.OriginX + dx
            Me.m_InsertImage.OriginY = Me.m_InsertImage.OriginY + dy
            prePoint.X = e.X
            prePoint.Y = e.Y
            Me.Refresh()
        End If
        OnInsertImageChanged(Me.m_InsertImage)
    End Sub

    Private Sub ImportImageControl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        'If e.Button = Windows.Forms.MouseButtons.Left Then
        '    Dim point As New Point(e.X, e.Y)
        '    If ReDrawSign = True Then
        '        '按用户拖动重新绘制

        '        Me.Refresh()
        '    ElseIf Me.m_MouseMoveType = 6 Then '设置切断区域
        '        If MsgBox("确定要设置新的坐标区域吗？", MsgBoxStyle.OkCancel, "设置坐标区域") = MsgBoxResult.Ok Then
        '            Dim PreP As PointF = PointToLocation(prePoint)
        '            Dim P As PointF = PointToLocation(point)

        '            If PreP.X < P.X Then
        '                ContourPaintSetting.ContourPannel.Axes.BottomAxis.MainAxisScale.Min = PreP.X
        '                ContourPaintSetting.ContourPannel.Axes.BottomAxis.MainAxisScale.Max = P.X
        '            Else
        '                ContourPaintSetting.ContourPannel.Axes.BottomAxis.MainAxisScale.Min = P.X
        '                ContourPaintSetting.ContourPannel.Axes.BottomAxis.MainAxisScale.Max = PreP.X
        '            End If

        '            If PreP.Y < P.Y Then
        '                ContourPaintSetting.ContourPannel.Axes.LeftAxis.MainAxisScale.Min = PreP.Y
        '                ContourPaintSetting.ContourPannel.Axes.LeftAxis.MainAxisScale.Max = P.Y
        '            Else
        '                ContourPaintSetting.ContourPannel.Axes.LeftAxis.MainAxisScale.Min = P.Y
        '                ContourPaintSetting.ContourPannel.Axes.LeftAxis.MainAxisScale.Max = PreP.Y
        '            End If
        '        End If

        '        Me.Refresh()
        '    End If
        'End If

    End Sub

    Private Sub ImportImageControl_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel

        If e.Delta > 0 Then '放大
            Me.zoomin(New Point(e.X, e.Y))
        ElseIf e.Delta < 0 Then '缩小
            Me.zoomout(New Point(e.X, e.Y))
        End If
        Dim point As New Point(e.X, e.Y)
        prePoint = point
        Me.Refresh()

    End Sub

    Private Sub ImportImageControl_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Me.m_InsertImage.DrawImage(e.Graphics)

    End Sub
End Class
